package ext.acae2.dms.web.input;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipException;

import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import wt.log4j.LogR;

public class UnzipProcessor {

    private static final Logger LOGGER = LogR.getLogger(UnzipProcessor.class.getName());
    
    public String unzip(File file) throws IOException {
        LOGGER.debug("> Start unzip");
        
        String filePath = file.getAbsolutePath();
        LOGGER.debug("filePath: " + filePath);
        
        if (!filePath.toUpperCase().endsWith(".ZIP")) {
            LOGGER.error("The file " + filePath + " is not zip type. return!");
            return "";
        }
        
        ZipFile zipFile = new ZipFile(filePath);
        
        String rootPath = makeRootFolder(filePath);
        makeZipFolder(rootPath, zipFile);
        String xmlPath = makeZipFile(rootPath, zipFile);
        
        LOGGER.debug("> End unzip");
        zipFile.close();
        return xmlPath;
    }
    
    private String makeRootFolder(String filePath) {
        String folderPath = filePath.substring(0, filePath.lastIndexOf("."));
        LOGGER.debug("folderPath: " + folderPath);
        
        File rootFolder = new File(folderPath);
        rootFolder.mkdirs();
        
        return folderPath;
    }
    
    private void makeZipFolder(String rootPath, ZipFile zipFile) {
        Enumeration<?> entries = zipFile.getEntries();
        
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            
            if (!entry.isDirectory()) {
                continue;
            }
            
            File folder = new File(rootPath + File.separator + entry.getName());
            folder.mkdirs();
            LOGGER.debug("mkdirs: " + folder.getAbsolutePath());
        }
    }
    
    private String makeZipFile(String rootPath, ZipFile zipFile) throws ZipException, IOException {
        Enumeration<?> entries = zipFile.getEntries();
        
        String xmlPath = "";
        
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            LOGGER.debug("entry name: " + entry.getName());
            
            String entryPath = rootPath + File.separator + entry.getName();
            LOGGER.debug("entryPath: " + entryPath);
            
            if (entry.isDirectory()) {
                continue;
            }
            
            if (entryPath.toUpperCase().endsWith(".XML")) {
                xmlPath = entryPath;
            }
            
            BufferedInputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
            BufferedOutputStream outputStream = new BufferedOutputStream(
                    new FileOutputStream(entryPath));
            
            int character = inputStream.read();
            while (character != -1) {
                outputStream.write(character);
                character = inputStream.read();
            }
            
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        }
        
        return xmlPath;
    }
}